/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelTelefone.java
 *
 * Created on 21/10/2011, 13:44:14
 */
package br.com.mocamorena.view.panel;

import br.com.mocamorena.model.Telefone;
import br.com.mocamorena.view.StateEntityWindow;
import br.com.mocamorena.view.dialog.TelefoneDialog;
import com.towel.swing.table.ObjectTableModel;
import java.util.List;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

/**
 *
 * @author Diego
 */
public class TelefonesPanel extends javax.swing.JPanel {

    private ObjectTableModel<Telefone> model;

    public TelefonesPanel() {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        addTelefoneButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        deleteTelefoneButton = new javax.swing.JButton();

        jLabel1.setText("Telefones:");

        addTelefoneButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/add.png"))); // NOI18N
        addTelefoneButton.setToolTipText("Adicionar novo telefone");
        addTelefoneButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addTelefoneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTelefoneButtonActionPerformed(evt);
            }
        });

        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(table);

        deleteTelefoneButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/mocamorena/resources/delete.png"))); // NOI18N
        deleteTelefoneButton.setToolTipText("Excluir telefone");
        deleteTelefoneButton.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        deleteTelefoneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteTelefoneButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(35, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(deleteTelefoneButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(addTelefoneButton, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(34, 34, 34))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addTelefoneButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteTelefoneButton, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addContainerGap(14, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addTelefoneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTelefoneButtonActionPerformed
        TelefoneDialog dialog = new TelefoneDialog(null, true, StateEntityWindow.NEW, null);
        dialog.setVisible(true);
        Telefone telefone = dialog.objetoTelefone();
        if (telefone != null) {
            model.add(telefone);
            int row = model.indexOf(telefone);
            table.setRowSelectionInterval(row, row);
        }
    }//GEN-LAST:event_addTelefoneButtonActionPerformed

    private void deleteTelefoneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteTelefoneButtonActionPerformed
        int row = table.getSelectedRow();
        if (row >= 0) {
            Telefone telefone = model.getValue(row);
            int index = model.indexOf(telefone);
            model.remove(index);
        }
    }//GEN-LAST:event_deleteTelefoneButtonActionPerformed

    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
        if (evt.getClickCount() == 2) {
            int row = table.getSelectedRow();
            if (row >= 0) {
                Telefone telefone = model.getValue(row);
                TelefoneDialog dialog = new TelefoneDialog(null, true, StateEntityWindow.UPDATE, telefone);
                dialog.setVisible(true);
                model.fireTableDataChanged();
                table.setRowSelectionInterval(row, row);
            }
        }
    }//GEN-LAST:event_tableMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addTelefoneButton;
    private javax.swing.JButton deleteTelefoneButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables

    public void setTelefonesList(List<Telefone> telefonesList) {
        model = new ObjectTableModel<Telefone>(Telefone.class, "tipo,ddd,numero,ramal");
        model.setData(telefonesList);
        table.setModel(model);
        organizeWindow();
    }

    public List<Telefone> getTelefonesList() {
        return model.getData();
    }

    private void organizeWindow() {
        TableColumn col0 = table.getColumnModel().getColumn(0);
        TableColumn col1 = table.getColumnModel().getColumn(1);
        TableColumn col2 = table.getColumnModel().getColumn(2);
        TableColumn col3 = table.getColumnModel().getColumn(3);

        DefaultTableCellRenderer headerRenderer = new DefaultTableCellRenderer();
        headerRenderer.setHorizontalAlignment(SwingConstants.CENTER);

        DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
        cellRenderer.setHorizontalAlignment(SwingConstants.CENTER);

        col0.setHeaderRenderer(headerRenderer);
        col1.setHeaderRenderer(headerRenderer);
        col2.setHeaderRenderer(headerRenderer);
        col3.setHeaderRenderer(headerRenderer);

        col0.setCellRenderer(cellRenderer);
        col1.setCellRenderer(cellRenderer);
        col2.setCellRenderer(cellRenderer);
        col3.setCellRenderer(cellRenderer);
    }
}
